package com.tdam.g12;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.AdapterView.OnItemClickListener;
import com.tdam.g12.adapters.*;
import com.tdam.g12.soporte.Constantes;
import com.tdam.g12.soporte.Contacto;

public class MainActivity extends ListActivity {
	
	private ContactosAdapter adapter;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        adapter = new ContactosAdapter(MainActivity.this);
        setListAdapter(adapter);
        
        loadContactos();
        setListenerOnItemClick();
    }
	
	private void setListenerOnItemClick ()
	{
		getListView().setOnItemClickListener( new OnItemClickListener() {
        	@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				onContactoSelected(position);
			}
		});
	}
	
	private void onContactoSelected(int position) 
    {
		Contacto contacto = (Contacto)adapter.getItem(position);
		if (contacto != null) {
			Intent intent = new Intent(this, MenuContacto.class);
			intent.putExtra(Constantes.ID_CONTACTO, contacto.getIdExterno());//setea el valor dentro de un HASH
			intent.putExtra(Constantes.NOMBRE, contacto.getNombre());
			intent.putExtra(Constantes.AVATAR, contacto.getAvatar());
			startActivity(intent);
		}
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_mainactivity, menu);
        return true;
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.opc_conhistorico:
			Intent intent = new Intent(this, ListaHistorialConectividad.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void loadContactos()
    {
    	//Contacto contacto = adapter.new ContactoInfo();
    	Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

		Contacto contacto = null;

		adapter.clear();
				
		while (cursor.moveToNext()) 
		{
			String idExterno = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
			String nombre = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			int avatar = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));

			if (idExterno != null) 
			{
				contacto = new Contacto(nombre, avatar, idExterno);
				adapter.addItem(contacto);
			}
		}
		
		cursor.close();
		
		adapter.notifyDataSetChanged();
    }
}
